/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package g360.WebXpdlValidation;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 *
 * @author andya
 */
public class ResourceManager
{
    private String getDataHome()
    {
        String dataHome = null;
        dataHome = "/g360/WebXpdlValidation/Data/";
        if (false) return "./webapps/Validate/data/";
        java.util.Properties p = System.getProperties();
        java.util.Enumeration keys = p.keys();
        String key, value;
        if (false)
        {
            key = "USER";
            value = System.getenv(key);
            key = "XPDL_SCHEMATA_PATH";
            value = System.getenv(key);
            if (value != null)
            {
                dataHome = value + "/";
            }
        }
        if (dataHome == null)
        {
            key = "catalina.home";
            value = p.getProperty(key);
            dataHome = value + "/webapps/XpdlValidator/data/";
        }
        if (dataHome == null)
        {
            dataHome = "/home/wfmcxpdl/appservers/apache-tomcat-6x/webapps/XpdlValidator/data/";
        }
        return dataHome;
    }

    public File getTransformFile_dontUse(String xpdlVersion)
    {
        File result = null;
        if (xpdlVersion.equals("2.0"))
        {
            result = new File(getDataHome() + "XPDL20Validation.xslt");
        }
        else
        {
            result = new File(getDataHome() + "XPDL21Validation.xslt");
        }
        return result;
    }

    public String getTransformFilePath(String xpdlVersion)
    {
        String result = null;
        if (xpdlVersion.equals("2.0"))
        {
            result = getDataHome() + "XPDL20Validation.xslt";
        }
        else
        {
            result = getDataHome() + "XPDL21Validation.xslt";
        }
        return result;
    }



    public String getG360SchemaFilePath(String xpdlVersion)
    {
        String result = null;
        result = this.getDataHome() + "xpdl20g360.xsd";
        return result;
    }

    public String getSchemaFilePath(String xpdlVersion)
    {
        String result = null;
        if (xpdlVersion.equals("1.0"))
        {
            result = this.getDataHome() + "TC-1025_schema_10_xpdl.xsd";
        }
        else if (xpdlVersion.equals("2.0"))
        {
            result = this.getDataHome() + "bpmnxpdl_24.xsd";
        }
        else if (xpdlVersion.equals("2.1"))
        {
            result = this.getDataHome() + "bpmnxpdl_31c.xsd";
        }
        return result;
    }


    public String getFormText()
    {
        String formText = ""; // <h2>here i am </h2>";
        String dataHome = this.getDataHome();
        String formTextFilePath = dataHome + "formText.txt";
        formText = this.loadTextFile(formTextFilePath, "form data");
        return formText;
    }

    public String getFooterText()
    {
        String commentsWelecomeTextFilePath = this.getDataHome() + "footerText.txt";
        String footerText = this.loadTextFile(commentsWelecomeTextFilePath, "footer data");
        return footerText;
    }

    public InputStream getInputStream(String path)
    {
        InputStream result = null;
        result = this.getClass().getResourceAsStream(path);
        return result;
    }

    public String loadTextFile(String resourcePath, String description)
    {
        String contents = "";
        FileReader fReader = null;
        try
        {
            InputStream inputStream = getInputStream(resourcePath);
            contents = inputStreamToString(inputStream);
        }
        catch (Exception ex)
        {
            contents += Html.makeTag("p", "Internal Error: Could not find " + description + " in " + resourcePath);
        }
        finally
        {
            try
            {
                if (fReader != null)
                {
                    fReader.close();
                }
            }
            catch (IOException ex)
            {
                contents += Html.makeTag("p", "Internal Error: Error when closing " + description + " file: " + resourcePath);
            }
        }
        return contents;
    }

    /** Read the entire content of a Reader into a String */
    public String readerToString(Reader is) throws IOException {
        StringBuffer sb = new StringBuffer( );
        int BLKSIZ = 1024;
        char[] b = new char[BLKSIZ];
        int n;

        // Read a block. If it gets any chars, append them.
        while ((n = is.read(b)) > 0) {
            sb.append(b, 0, n);
        }

        // Only construct the String object once, here.
        return sb.toString( );
    }

    /** Read the content of a Stream into a String */
    public String inputStreamToString(InputStream is)
    throws IOException {
        return readerToString(new InputStreamReader(is));
    }

    private String formTextResource =
            "<form enctype=\"multipart/form-data\" action=\"./ValidateXpdl\" method=post> \n " +
            "    <table border=\"0\" width=\"54%\" cellspacing=\"5\"> \n " +
            "    <tr><td width=\"4%\"></td> \n " +
            "    <td width=\"96%\">(Internal resource.)</td>  \n " +
            "    </tr> \n " +
            "        <tr> \n " +
            "            <td colspan=\"2\">Choose an XPDL file for validation: </td> \n " +
            "        </tr>  \n " +
            "        <tr>  \n " +
            "            <td colspan=\"2\"><input name=\"fileName\" type=\"file\" size=\"60\" id=\"file-edit\" onChange=\"updateSubmitButton()\"></td>  \n " +
            "        </tr>   \n " +
            "        <tr>  \n " +
            "            <td colspan=\"2\">  \n " +
            "                <input type=\"radio\" name=\"Override\" value=\"no\">Do not alter the file before validating.</input>  \n " +
            "            </td>  \n " +
            "        </tr>  \n " +
            "        <tr>  \n " +
            "            <td colspan=\"2\">  \n " +
            "                <input type=\"radio\" name=\"Override\" value=\"yes\" checked>Alter to file to use these settings:</input>  \n " +
            "            </td>  \n " +
            "        </tr>  \n " +
            "        <tr>  \n " +
            "            <td/>  \n " +
            "            <td >Schema Version:  \n " +
            "                <input type=radio name=\"Schema Version\" value=\"2.0\">2.0</input>  \n " +
            "                <input type=radio name=\"Schema Version\" value=\"2.1\" checked>2.1</input>  \n " +
            "            </td>  \n " +
            "        </tr>  \n " +
            "        <tr>  \n " +
            "            <td/>  \n " +
            "            <td>Validation Level:  \n " +
            "                <input type=radio name=\"Validation Level\" value=\"simple\">Simple</input>  \n " +
            "                <input type=radio name=\"Validation Level\" value=\"standard\" checked>Standard</input>  \n " +
            "                <input type=radio name=\"Validation Level\" value=\"complete\">Complete</input>  \n " +
            "            </td>  \n " +
            "        </tr>  \n " +
            "        <tr>  \n " +
            "            <td/>  \n " +
            "            <td>  \n " +
            "            </td>  \n " +
            "        </tr>  \n " +
            "        <tr></tr>  \n " +
            "        <tr>  \n " +
            "            <td colspan=\"2\" align=\"left\">  \n " +
            "                <input type=\"submit\" value=\"Upload and Validate\" id=\"submit-button\">  \n " +
            "            </td>  \n " +
            "        </tr>  \n " +
            "    </table>  \n " +
            "</form>"

;
}
